package util

import (
	"unsafe"
)

type Slice struct {
	Data unsafe.Pointer
	Len  int
	Cap  int
}

func Slice2Byte(s Slice, size int) []byte {
	s.Len *= size
	s.Cap *= size

	return *(*[]byte)(unsafe.Pointer(&s))
}

func Byte2Slice(b []byte, size int) *Slice {
	s := *(*Slice)(unsafe.Pointer(&b))
	s.Len /= size
	s.Cap /= size
	return &s
}
func Int64ToByte(src int64) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]int64{src}))), 8)
}

func Int32ToByte(src int32) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]int32{src}))), 4)
}

func Byte2Int32(src []byte) int32 {
	return (*(*[]int32)(unsafe.Pointer(Byte2Slice(src, 4))))[0]
}

func Byte2Int64(src []byte) int64 {
	return (*(*[]int64)(unsafe.Pointer(Byte2Slice(src, 8))))[0]
}

func Uint16ToByte(src uint16) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]uint16{src}))), 2)
}

func Uint32ToByte(src uint32) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]uint32{src}))), 4)
}

func Uint64ToByte(src uint64) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]uint64{src}))), 8)
}

func Byte2Uint32(src []byte) uint32 {
	return (*(*[]uint32)(unsafe.Pointer(Byte2Slice(src, 4))))[0]
}

func Byte2Uint64(src []byte) uint64 {
	return (*(*[]uint64)(unsafe.Pointer(Byte2Slice(src, 8))))[0]
}

func Byte2Uint16(src []byte) uint16 {
	return (*(*[]uint16)(unsafe.Pointer(Byte2Slice(src, 2))))[0]
}

func Float32ToByte(src float32) []byte {
	return Slice2Byte(*(*Slice)(unsafe.Pointer(&([]float32{src}))), 4)
}

func Byte2Float32(src []byte) float32 {
	return (*(*[]float32)(unsafe.Pointer(Byte2Slice(src, 4))))[0]
}
